package sjtu.pirate.util;

import java.util.List;
/**
 * @author wilbur.ma@qq.com
 * @date 2011.05.31
 */
public class StringUtil {
	public static String join(String sep, List<String> tokens) {
		return join(sep, tokens.toArray(new String[0]));
	}
	
	public static String join(String sep, String... tokens) {
		String str = tokens[0];
		for (int i=1;i<tokens.length;i++)
			str += sep + tokens[i];
		return str;
	}
	
	public static String joinPath(String... tokens) {
		String str = tokens[0];
		for (int i=1;i<tokens.length;i++)
			str += "/" + trimTail(tokens[i], "/|\\\\");
		return str;
	}
	
	public static String trimHead(String str, String delPattern) {
		return trimStr(str, delPattern, -1);
	}
	
	public static String trimTail(String str, String delPattern) {
		return trimStr(str, delPattern, 1);
	}
	
	public static String trim(String str, String delPattern) {
		return trimStr(str, delPattern, 0);
	}
	
	private static String trimStr(String str, String delPattern, int mode) {
		String result = str;
		if (mode != 1)
			result = result.replaceAll("^(" + delPattern + ")+", "");
		if (mode != -1)
			result = result.replaceAll("(" + delPattern + ")+$", "");
		return result;
	}
}
